<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="顾问姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入顾问姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:advisorUser:add']"
        >添加新顾问</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:advisorUser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          size="mini"
          @click="handleModal"
          v-hasPermi="['system:advisorUser:edit']"
        >顾问匹配规则</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="advisorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="顾问姓名" align="center" prop="userName" :show-overflow-tooltip="true" />
      <el-table-column label="电话" align="center" prop="phonenumber" :show-overflow-tooltip="true" />
      <el-table-column label="邮箱" align="center" prop="email" :show-overflow-tooltip="true" />
      <el-table-column label="微信号" align="center" prop="wxNumber" :show-overflow-tooltip="true" />
      <el-table-column label="服务客户数量" align="center" prop="serviceNumber" />
      <el-table-column label="权重" align="center" prop="weight">
        <template slot-scope="scope">
          <el-input
            class="el-sort"
            v-model="scope.row.weight"
            controls-position="right"
            @input="filter0_100(scope.row,'weight')"
            @blur="saveSort(scope.row)"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" width="160" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(1,scope.row)"
            v-hasPermi="['system:advisorUser:view']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(2,scope.row)"
            v-hasPermi="['system:advisorUser:edit']"
          >编辑</el-button>
          <el-button
            type="text"
            icon="el-icon-delete"
            size="mini"
            class="delete-btn"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:advisorUser:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 顾问匹配规则 弹框 -->
    <el-dialog title="顾问匹配规则" :visible.sync="open" width="500px" append-to-body>
      <el-form label-width="80px">
        <el-form-item label="规则选择">
          <el-radio-group v-model="ruleInfo.rule">
            <el-radio :label="1">平均分配</el-radio>
            <el-radio :label="2">按照权重比例划分</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAdvisor,
  delAdvisor,
  updateAdvisor,
  updateConfigAdvisor,
  getConfigAdvisor
} from "@/api/system/advisor";

export default {
  name: "Advisor",
  data() {
    return {
      ruleInfo: {},
      // 遮罩层
      loading: true,
      // 选中数组
      userIds: [],
      userNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户信息表格数据
      advisorList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: ""
      },
      // 表单参数
      form: {}
    };
  },
  created() {
    this.getList();
    this.getConfigAdvisor();
  },
  methods: {
    getConfigAdvisor() {
      getConfigAdvisor().then(res => {
        console.log(res);
        this.ruleInfo = res.data;
      });
    },
    /** 查询用户信息列表 */
    getList() {
      this.loading = true;
      listAdvisor(this.queryParams).then(response => {
        this.advisorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleModal() {
      this.open = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.userIds = selection.map(item => item.id);
      this.userNames = selection.map(item => item.userName);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        name: "AdvisorEdit",
        query: {
          type: 2
        }
      });
    },
    saveSort(item) {
      const postData = {
        ...item,
        advisorDetail: this.filterContent(item.advisorDetail)
      };
      updateAdvisor(postData).then(response => {
        if (response.code === 200) {
          this.msgSuccess("修改成功");
          this.getList();
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(type, row) {
      this.$router.push({
        name: "AdvisorEdit",
        query: {
          type,
          id: row.id
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      updateConfigAdvisor(this.ruleInfo).then(response => {
        if (response.code === 200) {
          this.msgSuccess("顾问匹配规则设置成功");
          this.open = false;
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.id || this.userIds;
      const userNames = row.userName || this.userNames;

      this.$confirm(
        '是否确认删除测评顾问"' + userNames + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delAdvisor(userIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    }
  }
};
</script>
